【学习笔记】[集训队互测 2021] 数列重排

其实这种纯贪心的题目是可以做出来的。

考虑怎么计算f(x)

首先,区间mexk等价于0k1都在区间中出现过。只考虑$

现在考虑插入k的数。你发现不是很好算,但是可以转化成计算插入一个数对答案的增量。具体的,每次可以看成是在长度为kmexk的子区间前面插入一个数,设这个数前面已经插入了xi个数,插入前序列总长度为len,那么答案为:

=len2(k1)xi

然后令xi增加1

现在我们考虑在两端插入数的情况。此时答案为:

=len(k1)xi

因此,排除掉前面那部分固定的贡献,初始将两端的xi:=k1,其余的置为0,我们可以将问题抽象成:每次选择一个数xi,令答案加上xi,然后xi减少1

暴力计算即可。复杂度O(m)

remark 对于这种纯贪心的题目,**大胆猜结论**。大多数情况下这样做都是对的,细节留到最后考虑。 其实还有几道纯贪心的题目: [CF887F Row of Models](https://www.luogu.com.cn/problem/CF887F) [CF1806F2 GCD Master (hard version)](https://www.luogu.com.cn/problem/CF1806F2) [CF1891E Brukhovich and Exams](https://codeforces.com/contest/1891/problem/E)(第一次ak div2) 反正我的感觉是结论**基本**都猜得到,但是有些细节考虑不到。比如GCD Master那道题就没有想到对于相同的数怎么处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define fi first
#define se second
using namespace std;
const int mod=998244353;
const int N=1e7+5;
int m,l,r,X;
ll sm[N],ans;
string str;
ll calc(ll l,ll r){
l%=mod,r%=mod;
return (r+l)*(r-l+1)%mod*(mod+1>>1)%mod;
}
void add(ll &x,ll y){
x=(x+y)%mod;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>m>>l>>r>>X>>str;
for(int i=1;i<=m;i++)sm[i]=sm[i-1]+X+str[i-1]-'0';
if(l==0){
ll x=sm[m]%mod;
ll y=x*(x+1)%mod*(mod+1>>1)%mod;
ans=y;
}
ll mul=1;int mi=X+1;
for(int i=1;i<=r;i++){
mul=mul*233%mod;
if(str[i-1]=='0')mi=X;
if(i>=l){
ll len=sm[i],x=sm[m]-sm[i],res=0;
add(res,calc(1,len-i+1));
add(res,calc(len,len+x-1));
add(res,-2*(i-1)*x);
if(x<=2*(i-1)){
ll a=x/2,b=x%2;
add(res,2*calc(i-a,i-1));
add(res,b*(i-1-a));
}
else{
x-=2*(i-1);
ll a=x/(mi+1),b=x%(mi+1);
add(res,2*calc(1,i-1));
add(res,-calc(1,a-1)*(mi+1));
add(res,-a*b);
}
res=res*mul%mod;
res=(res+mod)%mod;
ans^=res;
}
}cout<<ans;
}

【学习笔记】[集训队互测 2021] 数列重排

https://duanyu.netlify.app/2023/11/01/[集训队互测 2021]数列重排/

Author

duanyu

Posted on

2023-11-01

Updated on

2023-11-05

Licensed under

Comments